<?php 
class SessionMemcached {
	//有效时间
    protected $lifeTime=''; 
	//前辍
    protected $prefi='';
	//Memcached句柄
    protected $hander; 

    /**
     +----------------------------------------------------------
     * 打开Session 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @param string $savePath 
     * @param mixed $sessName  
     +----------------------------------------------------------
     */
    public function open($savePath, $sessName) { 
       $this->lifeTime = C('SESSION_EXPIRE');
	   $this->prefi	 =	 C('SESSION_PREFIX');
	   $memcache=new Memcache;
	   $memcache->connect(C("MEMCACHE_HOST"), C("MEMCACHE_PORT")) or die("could not connect!");
       if ($memcache){
	   		$this->hander=$memcache;
	   		return true; 
       }else{
       		return false;
       }
       
    } 

    /**
     +----------------------------------------------------------
     * 关闭Session 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
   public function close() { 
       $this->gc(ini_get('session.gc_maxlifetime')); 
       return $this->hander->close();
   } 

    /**
     +----------------------------------------------------------
     * 读取Session 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @param string $sessID 
     +----------------------------------------------------------
     */
   public function read($sessID) {
      $data=$this->hander->get($this->prefi.$sessID);
      return $data;
   } 

    /**
     +----------------------------------------------------------
     * 写入Session 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @param string $sessID 
     * @param String $sessData  
     +----------------------------------------------------------
     */
   public function write($sessID,$sessData) { 
       $expire = $this->lifeTime; 
       return $this->hander->set($this->prefi.$sessID, $sessData,$expire);
   } 

    /**
     +----------------------------------------------------------
     * 删除Session 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @param string $sessID 
     +----------------------------------------------------------
     */
   public function destroy($sessID) { 
      return $this->hander->delete($this->prefi.$sessID);
   } 

    /**
     +----------------------------------------------------------
     * Session 垃圾回收
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @param string $sessMaxLifeTime 
     +----------------------------------------------------------
     */
   public function gc($sessMaxLifeTime) { 
       return true;
   } 

    /**
     +----------------------------------------------------------
     * 初始化接口
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @param string $savePath 
     * @param mixed $sessName  
     +----------------------------------------------------------
     */
    public function execute() {
    	session_set_save_handler(array(&$this,"open"), 
                         array(&$this,"close"), 
                         array(&$this,"read"), 
                         array(&$this,"write"), 
                         array(&$this,"destroy"), 
                         array(&$this,"gc")); 

    }
}
?>
